package com.ruoyi.patient.service;

import com.ruoyi.patient.entity.*;
import com.ruoyi.service.domain.*;
import com.ruoyi.service.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;



@Component
public class GetCategoryItemsListService {

    @Autowired
    private ServiceCategoryMapper categoryMapper;

    @Autowired
    private ServiceCategoryitemsMapper categoryitemsMapper;

    @Autowired
    private ServicePackagescomboMapper packagescomboMapper;

    @Autowired
    private ServicePackagesMapper packagesMapper;

    @Autowired
    private ServiceItemsMapper itemsMapper;

    @Autowired
    private ServiceItemscomboMapper itemscomboMapper;

    @Autowired
    private ServiceComboMapper comboMapper;


    public List<CategoryListReturn> getCategoryItemsList()
    {
        List<CategoryListReturn> retCategoryList = new ArrayList<>();
        List<CategoryListReturn> newCategoryList = new ArrayList<>();
        List<ServiceCategory> oldCategoryList = categoryMapper.selectServiceCategoryListNew(null);
        for (ServiceCategory treeNode : oldCategoryList) {
            CategoryListReturn newCategoryNode = new CategoryListReturn(treeNode);
            newCategoryList.add(newCategoryNode);
        }
        ServiceCategoryitems serviceCategoryitems = new ServiceCategoryitems();
        serviceCategoryitems.setCategoryitemsUseFlag("0");
        serviceCategoryitems.setCategoryitemsDelFlag("0");
        List<ServiceCategoryitems> categoryitemsList = categoryitemsMapper.selectServiceCategoryitemsList(serviceCategoryitems);


        for (CategoryListReturn treeNode : newCategoryList) {
            for( ServiceCategoryitems item: categoryitemsList){
                if(item.getCategoryitemsCategoryid().equals(treeNode.getCategoryId())){
                    ServiceItems itemNode = itemsMapper.selectServiceItemsByItemId(item.getCategoryitemsItemid());
                    ItemsListReturn itemsCombosNode = new ItemsListReturn(itemNode);

                    ServiceItemscombo itemsComboSearch = new ServiceItemscombo();
                    itemsComboSearch.setItemscomboUseFlag("0");
                    itemsComboSearch.setItemscomboDelFlag("0");
                    itemsComboSearch.setItemscomboItemid(itemNode.getItemId());
                    List<ServiceItemscombo> itemscomboList = itemscomboMapper.selectServiceItemscomboList(itemsComboSearch);
                    for (ServiceItemscombo itemCombo : itemscomboList){
                        ServiceCombo combo = comboMapper.selectServiceComboByComboId(itemCombo.getItemscomboSuppliesid());

                        if (itemsCombosNode.getCombos() == null) {
                            itemsCombosNode.setCombos(new ArrayList<ServiceCombo>());
                        }
                        itemsCombosNode.getCombos().add(combo);
                    }

                    if (treeNode.getItems() == null) {
                        treeNode.setItems(new ArrayList<ItemsListReturn>());
                    }
                    treeNode.getItems().add(itemsCombosNode);
                }
            }
            if (null == treeNode.getCategoryLink() || "".equals(treeNode.getCategoryLink())) {
                retCategoryList.add(findChildren(treeNode, newCategoryList));
            }
        }
        return retCategoryList;
    }

    //public Page<CategoryListReturnVO> getCategoryListWithPagination(int page, int size) {
    public List<CategoryListReturnVO> getCategoryListWithPagination() {
        List<CategoryListReturn> retCategoryList = new ArrayList<>();
        List<CategoryListReturn> newCategoryList = new ArrayList<>();
        List<ServiceCategory> oldCategoryList = categoryMapper.selectServiceCategoryListNew(null);
        for (ServiceCategory treeNode : oldCategoryList) {
            CategoryListReturn newCategoryNode = new CategoryListReturn(treeNode);
            newCategoryList.add(newCategoryNode);
        }
        for (CategoryListReturn treeNode : newCategoryList) {
            if (null == treeNode.getCategoryLink() || "".equals(treeNode.getCategoryLink())) {
                retCategoryList.add(findChildren(treeNode, newCategoryList));
            }
        }
        List<CategoryListReturn> newList = new ArrayList<>();
        newList.add(retCategoryList.get(0));
        List<CategoryListReturnVO> ret = remakeChildren(newList);
        ret = ret.get(0).getChildren();

        //// 分页逻辑
        //Pageable pageable = PageRequest.of(page, size);
        //int start = (int) pageable.getOffset();
        //int end = Math.min((start + pageable.getPageSize()), ret.size());
        //List<CategoryListReturnVO> pagedCategoryList = ret.subList(start, end);

        //return new PageImpl<>(pagedCategoryList, pageable, ret.size());
        return ret;
    }

    public Page<ItemComboReturnVO> getCategoryItemsListWithPagination(Long categoryId, String keywords, int page, int size) {
        List<ItemsListReturn> itemsList = new ArrayList<>();
        List<ItemComboReturnVO> retList = new ArrayList<ItemComboReturnVO>();
        ServiceCategoryitems serviceCategoryitems = new ServiceCategoryitems();
        serviceCategoryitems.setCategoryitemsUseFlag("0");
        serviceCategoryitems.setCategoryitemsDelFlag("0");
        List<ServiceCategoryitems> categoryitemsList = categoryitemsMapper.selectServiceCategoryitemsList(serviceCategoryitems);

        for (ServiceCategoryitems item : categoryitemsList) {
            if (item.getCategoryitemsCategoryid().equals(categoryId)) {
                ServiceItems searchItem = new ServiceItems();
                searchItem.setItemId(item.getCategoryitemsItemid());
                searchItem.setItemName(keywords);
                searchItem.setItemUseFlag("0");
                searchItem.setItemDelFlag("0");
                //List<ServiceItems> tempData = itemsMapper.selectServiceItemsList(searchItem);
                List<ServiceItems> itemList = itemsMapper.selectServiceItemsList(searchItem);
                if (itemList.size() == 0){
                    continue;
                }
                ServiceItems itemNode = itemList.get(0);
                ItemsListReturn itemsCombosNode = new ItemsListReturn(itemNode);
                ItemComboReturnVO retNode = new ItemComboReturnVO(itemNode);
                ServiceCategory category = categoryMapper.selectServiceCategoryByCategoryId(categoryId);
                retNode.setCateName(category.getCategoryName());

                ServiceItemscombo itemsComboSearch = new ServiceItemscombo();
                itemsComboSearch.setItemscomboUseFlag("0");
                itemsComboSearch.setItemscomboDelFlag("0");
                itemsComboSearch.setItemscomboItemid(itemNode.getItemId());
                List<ServiceItemscombo> itemscomboList = itemscomboMapper.selectServiceItemscomboList(itemsComboSearch);

                for (ServiceItemscombo itemCombo : itemscomboList) {
                    ServiceCombo combo = comboMapper.selectServiceComboByComboId(itemCombo.getItemscomboSuppliesid());
                    if (retNode.getPrice() == null || retNode.getPrice() > combo.getComboPrice()){
                        retNode.setPrice(combo.getComboPrice());
                    }
                    ComboPackageReturnVO retCombo = new ComboPackageReturnVO(combo);
                    ServicePackagescombo searchPackagesCombo = new ServicePackagescombo();
                    searchPackagesCombo.setPackagescomboComboid(combo.getComboId());
                    List<ServicePackagescombo> packagescomboList = packagescomboMapper.selectServicePackagescomboList(searchPackagesCombo);
                    for (ServicePackagescombo packagescombo : packagescomboList){
                        ServicePackages packages = packagesMapper.selectServicePackagesByPackagesId(packagescombo.getPackagescomboPackagesid());
                        ServicePackagesReturnVO retPackage = new ServicePackagesReturnVO(packages);

                        if (retCombo.getPackages() == null) {
                            retCombo.setPackages(new ArrayList<ServicePackagesReturnVO>());
                        }
                        retCombo.getPackages().add(retPackage);
                    }

                    if (retNode.getSpecifications() == null) {
                        retNode.setSpecifications(new ArrayList<ComboPackageReturnVO>());
                    }
                    retNode.getSpecifications().add(retCombo);
                }

                retList.add(retNode);
            }
        }

        // 分页逻辑
        Pageable pageable = PageRequest.of(page, size);
        int start = (int) pageable.getOffset();
        int end = Math.min((start + pageable.getPageSize()), retList.size());
        List<ItemComboReturnVO> pagedItemsList = retList.subList(start, end);

        return new PageImpl<>(pagedItemsList, pageable, retList.size());
    }



    static CategoryListReturn findChildren(CategoryListReturn category, List<CategoryListReturn> categoryList) {
        for (CategoryListReturn item : categoryList) {
            if (category.getCategoryId().equals(item.getCategoryLink())) {
                if (category.getChildren() == null) {
                    category.setChildren(new ArrayList<CategoryListReturn>());
                }
                category.getChildren().add(findChildren(item, categoryList));
            }
        }
        return category;
    }

    static List<CategoryListReturnVO> remakeChildren(List<CategoryListReturn> categoryList) {
        List<CategoryListReturnVO> retCategoryList = new ArrayList<CategoryListReturnVO>();
        for (CategoryListReturn item : categoryList) {
            CategoryListReturnVO categoryListReturnVO = new CategoryListReturnVO(item);
            if (item.getChildren() != null) {
                categoryListReturnVO.setChildren(remakeChildren(item.getChildren()));
            }
            retCategoryList.add(categoryListReturnVO);
        }
        return retCategoryList;
    }


    public  Page<ItemComboReturnVO>  getItemsListBySearch(String searchName, int page, int size){
        List<ItemsListReturn> itemsList = new ArrayList<>();
        List<ItemComboReturnVO> retList = new ArrayList<ItemComboReturnVO>();

        //for (ServiceCategoryitems item : categoryitemsList) {
        ServiceItems searchItem = new ServiceItems();
        //searchItem.setItemId(item.getCategoryitemsItemid());
        searchItem.setItemName(searchName);
        searchItem.setItemUseFlag("0");
        searchItem.setItemDelFlag("0");
        List<ServiceItems> items = itemsMapper.selectServiceItemsList(searchItem);
        for (ServiceItems itemNode : items){
            ItemsListReturn itemsCombosNode = new ItemsListReturn(itemNode);
            ItemComboReturnVO retNode = new ItemComboReturnVO(itemNode);

            ServiceCategoryitems serviceCategoryitems = new ServiceCategoryitems();
            serviceCategoryitems.setCategoryitemsUseFlag("0");
            serviceCategoryitems.setCategoryitemsDelFlag("0");
            serviceCategoryitems.setCategoryitemsItemid(itemNode.getItemId());
            List<ServiceCategoryitems> categoryitemsList = categoryitemsMapper.selectServiceCategoryitemsList(serviceCategoryitems);
            if (categoryitemsList.size() > 0){
                ServiceCategoryitems item = categoryitemsList.get(0);
                ServiceCategory category = categoryMapper.selectServiceCategoryByCategoryId(item.getCategoryitemsCategoryid());
                retNode.setCateName(category.getCategoryName());
            }

            ServiceItemscombo itemsComboSearch = new ServiceItemscombo();
            itemsComboSearch.setItemscomboUseFlag("0");
            itemsComboSearch.setItemscomboDelFlag("0");
            itemsComboSearch.setItemscomboItemid(itemNode.getItemId());
            List<ServiceItemscombo> itemscomboList = itemscomboMapper.selectServiceItemscomboList(itemsComboSearch);

            for (ServiceItemscombo itemCombo : itemscomboList) {
                ServiceCombo combo = comboMapper.selectServiceComboByComboId(itemCombo.getItemscomboSuppliesid());
                if (retNode.getPrice() == null || retNode.getPrice() > combo.getComboPrice()){
                    retNode.setPrice(combo.getComboPrice());
                }
                ComboPackageReturnVO retCombo = new ComboPackageReturnVO(combo);
                ServicePackagescombo searchPackagesCombo = new ServicePackagescombo();
                searchPackagesCombo.setPackagescomboComboid(combo.getComboId());
                List<ServicePackagescombo> packagescomboList = packagescomboMapper.selectServicePackagescomboList(searchPackagesCombo);
                for (ServicePackagescombo packagescombo : packagescomboList){
                    ServicePackages packages = packagesMapper.selectServicePackagesByPackagesId(packagescombo.getPackagescomboPackagesid());
                    ServicePackagesReturnVO retPackage = new ServicePackagesReturnVO(packages);

                    if (retCombo.getPackages() == null) {
                        retCombo.setPackages(new ArrayList<ServicePackagesReturnVO>());
                    }
                    retCombo.getPackages().add(retPackage);
                }

                if (retNode.getSpecifications() == null) {
                    retNode.setSpecifications(new ArrayList<ComboPackageReturnVO>());
                }
                retNode.getSpecifications().add(retCombo);
            }

            retList.add(retNode);
        }

        //}
        // 分页逻辑
        Pageable pageable = PageRequest.of(page, size);
        int start = (int) pageable.getOffset();
        int end = Math.min((start + pageable.getPageSize()), retList.size());
        List<ItemComboReturnVO> pagedItemsList = retList.subList(start, end);

        return new PageImpl<>(pagedItemsList, pageable, retList.size());
    }

    public List<ItemsListReturn> getItemsDetailByItemIds(Long[] itemIds){
        List<ItemsListReturn> retItemsList = new ArrayList<>();
        for (Long itemId : itemIds){
            ServiceItems oldItem = itemsMapper.selectServiceItemsByItemId(itemId);
            ItemsListReturn newItem = new ItemsListReturn(oldItem);
            retItemsList.add(newItem);
        }

        for (ItemsListReturn newItem : retItemsList){
            ServiceItemscombo itemsComboSearch = new ServiceItemscombo();
            itemsComboSearch.setItemscomboUseFlag("0");
            itemsComboSearch.setItemscomboDelFlag("0");
            itemsComboSearch.setItemscomboItemid(newItem.getItemId());
            List<ServiceItemscombo> itemscomboList = itemscomboMapper.selectServiceItemscomboList(itemsComboSearch);
            for (ServiceItemscombo itemCombo : itemscomboList){
                ServiceCombo combo = comboMapper.selectServiceComboByComboId(itemCombo.getItemscomboSuppliesid());

                if (newItem.getCombos() == null) {
                    newItem.setCombos(new ArrayList<ServiceCombo>());
                }
                newItem.getCombos().add(combo);
            }

        }

        return retItemsList;
    }

    public List<ItemsListReturn> getItemsDetailByItemIds(Long itemId){
        List<ItemsListReturn> retItemsList = new ArrayList<>();
        ServiceItems oldItem = itemsMapper.selectServiceItemsByItemId(itemId);
        ItemsListReturn item = new ItemsListReturn(oldItem);
        retItemsList.add(item);

        for (ItemsListReturn newItem : retItemsList){
            ServiceItemscombo itemsComboSearch = new ServiceItemscombo();
            itemsComboSearch.setItemscomboUseFlag("0");
            itemsComboSearch.setItemscomboDelFlag("0");
            itemsComboSearch.setItemscomboItemid(newItem.getItemId());
            List<ServiceItemscombo> itemscomboList = itemscomboMapper.selectServiceItemscomboList(itemsComboSearch);
            for (ServiceItemscombo itemCombo : itemscomboList){
                ServiceCombo combo = comboMapper.selectServiceComboByComboId(itemCombo.getItemscomboSuppliesid());

                if (newItem.getCombos() == null) {
                    newItem.setCombos(new ArrayList<ServiceCombo>());
                }
                newItem.getCombos().add(combo);
            }

        }

        return retItemsList;
    }

}
